package com.Web.Class;

import com.DAO.Impl.ClassDAOImpl;
import com.DAO.Impl.ClassTypeImpl;
import com.DAO.Impl.StudentDAOImpl;
import com.DAO.Impl.coachesDAOImpl;
import com.DAO.StudentDAO;
import com.entity.Class;
import com.entity.Student;
import com.google.gson.Gson;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 20x
 * @date 2024 - 11 -27
 **/
@WebServlet("/ListClass")
public class ListClassServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();
        Gson gson = new Gson();

        String classType = req.getParameter("classType")==null ? "篮球" :req.getParameter("classType");

        String weekOfMonth = req.getParameter("weekOfMonth");
        List<Class> classList = new ClassDAOImpl().select(classType, weekOfMonth);
        List<String> classCoaches = new coachesDAOImpl().coachesNames();
        List<Student> studentList = new StudentDAOImpl().ListStudent();
        String message = "获取数据失败";
        if(classList.size() >= 0){
            message = "获取数据成功";
        }
        // 创建一个包含所有需要发送的数据的对象
        Map<String, Object> responseData = new HashMap<>();
        responseData.put("classList", classList);
        responseData.put("message", message);
        responseData.put("classCoaches", classCoaches);
        responseData.put("studentList", studentList);
        responseData.put("ClassAll",new ClassDAOImpl().select(classType, ""));
        // 将对象转换为JSON字符串
        String jsonResponse = gson.toJson(responseData);
        out.write(jsonResponse);
        out.close();
    }
}
